package com.crb.crm.manage.message.dingding;

import com.crb.crm.manage.message.common.SAGTemplate;
import com.crb.crm.manage.message.common.SMGChannel;
import com.crb.crm.manage.message.common.SMGSender;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 * Package: com.gulimall.thrid.party.msg
 * Description：dingding消息推送
 * Author: huangjian
 * Date:  2020/6/18 14:51
 * Modified By:
 */
@Component
class DingdingMGSender extends SMGSender {
    @Autowired
    private DingDingTools dingDingTools;

    @Resource
    private DingdingMessageTemplate dingdingMessageTemplate;

    private String content;
    @Override
    public SMGChannel support() {
        return SMGChannel.DING_DING;
    }

    @Override
    public String content() {
        return content;
    }

    @Override
    protected void sendMsg(SAGTemplate templateId, String signature, Map<String, String> param, String reviver) {
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        at.setAtMobiles(Arrays.asList(reviver));
        //渲染變量
        String content = dingdingMessageTemplate.formatVars(templateId.name(), param, signature);
        this.content=content;
        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
        text.setContent(content);
        try {
            dingDingTools.sendMsg(text, null);
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }

}
